package com.exp.controller;

import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;

import com.exp.model.User;
import com.exp.service.UserService;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class UserController extends Controller {
	private static final UserService userService = new UserService();

	public void index() {
		view();
	}

	public void view() {
		render("list.jsp");
	}

	public void list() {
		String sql = "from t_user ";
		if (getPara("name") != null && !"".equals(getPara("name"))) {
			// commons-lang防止sql注入
			String name = StringEscapeUtils.escapeSql(getPara("name"));
			sql += " where name like '%" + name + "%' ";
		}
		renderJson("data", User.dao.paginate(getParaToInt("pageNum", 1), 10,
				"select *", sql));

	}

	public void editUI() {
		Integer id = getParaToInt(0);
		if (id != null) {
			User user = User.dao.findById(id);
			setAttr("user", user);
		}
		render("edit.jsp");
	}

	public void edit() {
		User user = getModel(User.class);
		if (user.get("id") != null) {
			user.update();
		} else {
			user.save();
		}
		view();
	}

	public void delete() {
		User.dao.deleteById(getParaToInt(0));
		view();
	}

}
